package www.wxwnb.clerk.ui.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import www.wxwnb.clerk.R;
import www.wxwnb.clerk.adapter.MsgQueryAdapter;
import www.wxwnb.clerk.base.BaseActivity;
import www.wxwnb.clerk.http.HttpMethods;
import www.wxwnb.clerk.http.HttpService;
import www.wxwnb.clerk.http.entity.ReviewDetailBean;
import www.wxwnb.clerk.utils.TypeAndStateUtil;

public class ReviewDetailActivity extends BaseActivity {

    private ImageButton ibtn_back;
    private TextView tv_title;
    private TextView tv_loginName, tv_password, tv_phoneNum,
            tv_storeType, tv_address,
            tv_money, tv_upStore, tv_code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_review_detail);
    }

    @Override
    protected void initView() {
        ibtn_back = (ImageButton) findViewById(R.id.ibtn_back);
        tv_title = (TextView) findViewById(R.id.tv_title);

        tv_loginName = (TextView) findViewById(R.id.tv_loginName);
        tv_password = (TextView) findViewById(R.id.tv_password);
        tv_phoneNum = (TextView) findViewById(R.id.tv_phoneNum);

        tv_storeType = (TextView) findViewById(R.id.tv_storeType);
        tv_address = (TextView) findViewById(R.id.tv_address);

        tv_money = (TextView) findViewById(R.id.tv_money);
        tv_upStore = (TextView) findViewById(R.id.tv_upStore);
        tv_code = (TextView) findViewById(R.id.tv_code);
    }

    @Override
    public void initListener() {
        ibtn_back.setOnClickListener(this);
    }

    @Override
    public void initData() {
        tv_title.setText(R.string.review_details);
        Intent intent = getIntent();
        int id = intent.getIntExtra(MsgQueryAdapter.ID, -1);
        getDataFromServer(String.valueOf(id));
    }

    private void getDataFromServer(String id) {
        HttpService httpService = HttpMethods.getInstance().mRetrofit.create(HttpService.class);
        Call<ReviewDetailBean> call = httpService.getReviewDetails(id);
        call.enqueue(new Callback<ReviewDetailBean>() {
            @Override
            public void onResponse(Call<ReviewDetailBean> call, Response<ReviewDetailBean> response) {
                if (response.isSuccessful()) {
                    if (response.body().getReturnCode() == 1) {
                        handlerSuccess(response.body().getData());
                    }
                }
            }

            @Override
            public void onFailure(Call<ReviewDetailBean> call, Throwable t) {

            }
        });
    }

    private void handlerSuccess(ReviewDetailBean.DataBean data) {
        tv_loginName.setText(data.getLoginName());
        tv_password.setText(data.getPassword());
        tv_phoneNum.setText(data.getPhone());

        tv_storeType.setText(TypeAndStateUtil.getTypeByInt(data.getType()));
        tv_address.setText(data.getAddress());

        tv_money.setText(String.valueOf(data.getMonery()));
        tv_upStore.setText(String.valueOf(data.getParentChief()));
        tv_code.setText(TypeAndStateUtil.getStateByInt(data.getState()));
    }

    @Override
    public void processClick(View v) {
        switch (v.getId()) {
            case R.id.ibtn_back:
                finish();
                break;
        }
    }
}
